<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
  <meta name="referrer" content="never">
  <title>offsetTop&Left</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    ul {
      list-style: none;
    }

    .box {
      margin: 100px;
    }

    .inner {
      width: 100px;
      height: 100px;
      background-color: #f00;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="inner"></div>
  </div>
  <script type="text/javascript">
    var inner = document.querySelector(".inner");

    console.log("inner.offsetParent => body,即offsetTop/Left返回的是inner与页面的上左距离");
    console.log({
      t: inner.offsetTop,
      l: inner.offsetLeft
    });

    console.log("box 添加定位");
    inner.parentNode.style.position = "relative";
    console.log({
      t: inner.offsetTop,
      l: inner.offsetLeft
    });

    console.log("box 添加内边距");
    inner.parentNode.style.padding = "20px";
    console.log({
      t: inner.offsetTop,
      l: inner.offsetLeft
    });

    console.log("box 添加边框");
    inner.parentNode.style.border = "20px solid #0f0";
    console.log({
      t: inner.offsetTop,
      l: inner.offsetLeft
    });

    console.log("inner 添加外边距");
    inner.style.margin = "20px";
    console.log({
      t: inner.offsetTop,
      l: inner.offsetLeft
    });

    console.log("set 小数");
    inner.style.margin = "20.73px";
    console.log({
      t: inner.offsetTop,
      l: inner.offsetLeft
    });

    console.log("直接赋值");
    inner.offsetTop = 400;
    console.log({
      t: inner.offsetTop,
      l: inner.offsetLeft
    });
  </script>
</body>
</html>